Overview:
- One or more possible subsets can be created or derived from an existing numpy.ndarray object.
- The created subsets will be numpy.ndarray objects.
- Based on the requirements, care should be taken to ensure that returned objects are copies.
- Creating such subsets from an existing numpy.ndarray object is based on several criteria. For example, a subset of an ndarray can be created from the elements of a diagonal, or one dimension can be omitted of an array if there is only one element in that dimension.
- Elements can be extracted from a numpy.ndarray object based on Boolean conditions for each of the indices or based on an array of indices.
- The numpy.ndarray provides several methods that help creating ndarray objects with a subset of elements from an existing ndarray object.
- These methods take various criteria such as selected index of an array or a specific index of a diagonal and so on.
- Few such methods of numpy.ndarray include compress(), diagonal(), squeeze() and take() which are given focus here.
- Various modes like clipping of values, wrapping around of indices are supported in take() method of numpy.ndarray.
Select each ndarray element based on a Boolean condition:
- Using the compress() method, for a given numpy.ndarray object each of its element can be specified a Boolean expression based on which the presence or the absence of that element in the new array is decided.
- The Boolean expressions are specified as a flat 1-dimensional array.
- Elements can be selected based on axis or without specifying any axis.
Example:numpy.ndarray.compress
import numpy as np
# define a 3-dimensional ndarray object array_3d = np.array([[[10,20,30], [40,50,60], [70,80,90]],
[[100,110,120], [130,140,150], [160,170,180]],
[[190,200,210], [220,230,240], [250,260,270]]])
# Consider the 3d array as a flattened array # Select only first 3 elements compressedArray = array_3d.compress([1, 1, 1]) print("Selecting first 3 elements from a 3-dimensaional array") print(compressedArray)
# Do not select the first two elements but select the next two elements print("Selecting 5 elements from the beginning but excluding the first 2 elements") compressedArray = array_3d.compress([0, 0, 1, 1, 1]) print(compressedArray)
# Consider the 3d array as a 3d array and select element elements on a given axis # Select elements on axis 0 compressedArray = array_3d.compress([1,1,1], axis=0) print("Elements selected along axis 0:") print(compressedArray)
# Select elements on axis 1 compressedArray = array_3d.compress([1,1,1], axis=0) print("Elements selected along axis 1:") print(compressedArray)
# Select elements on axis 2 compressedArray = array_3d.compress([1,1,1], axis=0) print("Elements selected along axis 2:") print(compressedArray)
|
Output:
Selecting first 3 elements from a 3-dimensaional array [10 20 30] Selecting 5 elements from the beginning but excluding the first 2 elements [30 40 50] Elements selected along axis 0: [[[ 10 20 30] [ 40 50 60] [ 70 80 90]]
[[100 110 120] [130 140 150] [160 170 180]]
[[190 200 210] [220 230 240] [250 260 270]]] Elements selected along axis 1: [[[ 10 20 30] [ 40 50 60] [ 70 80 90]]
[[100 110 120] [130 140 150] [160 170 180]]
[[190 200 210] [220 230 240] [250 260 270]]] Elements selected along axis 2: [[[ 10 20 30] [ 40 50 60] [ 70 80 90]]
[[100 110 120] [130 140 150] [160 170 180]]
[[190 200 210] [220 230 240] [250 260 270]]] |
Remove a redundant dimension from ndarray object:
- If an array needs to be reduced of a dimension, when it has only one element in that dimension - the method squeeze() of ndarray can be used.
Example: numpy.ndarray.squeeze()
import numpy as np
# A 2-dimensional array with only one 1d array inside array_2d = np.array([[1,2,4]]) squeezed = array_2d.squeeze()
print("Input 2-dimensional Array:") print(array_2d)
# Print the array that has been reduced of a dimension print("Output after a 2-dimensional array is reduced of a redundant dimension:") print(squeezed)
# A 3-dimensional array with only one 2d array inside array_3d = np.array([[[1,2,4], [2,2,4]]])
print("Input 3-dimensional Array:") print(array_3d)
# Print the array that has been reduced of a dimension squeezed = array_3d.squeeze()
print("Output after a 3-dimensional array is reduced of a redundant dimension:") print(squeezed) |
Output:
Input 2-dimensional Array: [[1 2 4]] Output after a 2-dimensional array is reduced of a redundant dimension: [1 2 4] Input 3-dimensional Array: [[[1 2 4] [2 2 4]]] Output after a 3-dimensional array is reduced of a redundant dimension: [[1 2 4] [2 2 4]] |
Retrieve diagonals from an n-dimensional array using numpy.ndarray:
- The diagonal() method can be used to retrieve diagonals of multidimensional arrays and matrixes.
- Developers need to take care of examining whether the returned array is a copy or a view and create a copy if necessary.
Example:
import numpy as np
# Define a 3-dimensional array - an ndarray object using numpy.array method ndarray_3d = np.array([[[2,4,6], [8,10,12], [14,16,18]],
[[20,22,24], [26,28,30], [32,34,36]],
[[38,40,42], [44,46,48], [50,52,54]]] ) print("Input 3-d array:") print(ndarray_3d)
print("Output of ndarray.diagonal on a 3 dimensional matrix")
print("Axis1:0, Axis2:1") print(ndarray_3d.diagonal(axis1=0,axis2=1))
print("Axis1:1, Axis2:2") print(ndarray_3d.diagonal(axis1=1,axis2=2))
print("Axis1:2, Axis2:1") print(ndarray_3d.diagonal(axis1=0,axis2=2))
ndarray_2d = np.array([[2,4,6], [8,10,12]])
print("Input 2-d array:") print(ndarray_2d)
# Diagonal offset = 0 print("Digaonal offset:0") print(ndarray_2d.diagonal(0))
# Diagonal offset = -1 print("Digaonal offset:-1") ndarray_2d = np.array([[2,4,6], [8,10,12]])
print(ndarray_2d.diagonal(-1)) |
Output:
Input 3-d array: [[[ 2 4 6] [ 8 10 12] [14 16 18]]
[[20 22 24] [26 28 30] [32 34 36]]
[[38 40 42] [44 46 48] [50 52 54]]] Output of ndarray.diagonal on a 3 dimensional matrix Axis1:0, Axis2:1 [[ 2 26 50] [ 4 28 52] [ 6 30 54]] Axis1:1, Axis2:2 [[ 2 10 18] [20 28 36] [38 46 54]] Axis1:2, Axis2:1 [[ 2 22 42] [ 8 28 48] [14 34 54]] Input 2-d array: [[ 2 4 6] [ 8 10 12]] Digaonal offset:0 [ 2 10] Digaonal offset:-1 [8] |
Extract elements by specifying an array of indices:
- The take() method of numpy.ndarray is similar to the compress() method of numpy.ndarray. However, unlike compress() method that accepts boolean expressions for extracting values from a specific index of the ndarray the take method accepts an array of indices whose values will be returned.
- The wrap mode enables rotating from the beginning of the array if the indices specified are greater than or equal to the number of elements present in the input array.
- In clip mode if an index is are greater than or equal to the number of elements present in the input array the value for that index will be replaced by the value of last index on that axis.
Example:
import numpy as np
# Extract values from an 1-dimensional ndarray object print("Input 1-dimensional array:") array_1d = np.array([10,20,30,40,50]) print(array_1d)
indices = np.array([1,2,3]) print("Array of indices:") print(indices)
subset = array_1d.take(indices)
print("Values from selected indices of an one dimensional ndarray object:") print(subset)
indices = np.array([3,4,5]) subset = array_1d.take(indices, mode='wrap')
print("Array of indices:") print(indices) print("Array Extracted from ndarray using take() method with wrap Mode enbaled:") print(subset)
# Extract values from an 2-dimensional ndarray object array2d = np.array([[1,3,5], [7,9,11], [13,15,17]]) print("Input 2-dimensional ndarray object:") print(array2d) indices = np.array([[0,2],[3,5],[8,9]]) subset = array2d.take(indices, mode='wrap') print("2-d array extracted from the input 2-d array using take() method with wrap Mode enbaled:") print(subset) |
Output:
Input 1-dimensional array: [10 20 30 40 50] Array of indices: [1 2 3] Values from selected indices of an one dimensional ndarray object: [20 30 40] Array of indices: [3 4 5] Array Extracted from ndarray using take() method with wrap Mode enbaled: [40 50 10] Input 2-dimensional ndarray object: [[ 1 3 5] [ 7 9 11] [13 15 17]] 2-d array extracted from the input 2-d array using take() method with wrap Mode enbaled: [[ 1 5] [ 7 11] [17 1]]
|